package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.bo.backgroundv2.ProjectBo;
import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.ProjectParam;
import com.dmap.admin.controller.backgroundv2.param.ProjectSelectParam;
import com.dmap.admin.dao.ProjectMapper;
import com.dmap.admin.po.Project;
import com.dmap.admin.po.ProjectExample;
import com.dmap.admin.service.backgroundv2.ProjectService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 25/05/18
 * \* Description:项目相关接口实现类
 * \
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public Project insertProject(ProjectParam param) {
        Project project = new Project();
        if (StringUtils.isNotBlank(param.getAppId()) && StringUtils.isNumeric(param.getAppId())) {
            project.setAppId(Long.valueOf(param.getAppId()));
        }
        if (StringUtils.isNotBlank(param.getDeviceTypeId()) && StringUtils.isNumeric(param.getDeviceTypeId())) {
            project.setDeviceTypeId(Long.valueOf(param.getDeviceTypeId()));
        }
        if (StringUtils.isNotBlank(param.getDeviceId()) && StringUtils.isNumeric(param.getDeviceId())) {
            project.setDeviceId(Long.valueOf(param.getDeviceId()));
        }
        if (StringUtils.isNotBlank(param.getCompanyId()) && StringUtils.isNumeric(param.getCompanyId())) {
            project.setCompanyId(Long.valueOf(param.getCompanyId()));
        }
        project.setCompanyNumber(param.getCompanyNumber());
        project.setCompanyName(param.getCompanyName());
        project.setCompanyType(param.getCompanyType());
        project.setStatus(AdminConstant.NORMAL_STATUS);
        project.setUserType(param.getUserType());
        if (StringUtils.isNotBlank(param.getCreatorId()) && StringUtils.isNumeric(param.getCreatorId())) {
            project.setCreatorId(Long.valueOf(param.getCreatorId()));
            project.setEditorId(project.getCreatorId());
        }
        project.setCreatorAccount(param.getCreatorAccount());
        project.setEditorAccount(project.getCreatorAccount());
        project.setCreateTime(System.currentTimeMillis() / 1000);
        project.setUpdateTime(project.getCreateTime());
        int success = projectMapper.insertSelective(project);
        return (success == 1) ? project : null;
    }

    @Override
    public boolean updateProject(ProjectParam param) {
        Project project = new Project();
        project.setId(Long.valueOf(param.getId()));
        if (StringUtils.isNotBlank(param.getAppId()) && StringUtils.isNumeric(param.getAppId())) {
            project.setAppId(Long.valueOf(param.getAppId()));
        }
        if (StringUtils.isNotBlank(param.getDeviceTypeId()) && StringUtils.isNumeric(param.getDeviceTypeId())) {
            project.setDeviceTypeId(Long.valueOf(param.getDeviceTypeId()));
        }
        if (StringUtils.isNotBlank(param.getDeviceId()) && StringUtils.isNumeric(param.getDeviceId())) {
            project.setDeviceId(Long.valueOf(param.getDeviceId()));
        }
        if (StringUtils.isNotBlank(param.getCompanyId()) && StringUtils.isNumeric(param.getCompanyId())) {
            project.setCompanyId(Long.valueOf(param.getCompanyId()));
        }
        project.setCompanyNumber(param.getCompanyNumber());
        project.setCompanyName(param.getCompanyName());
        project.setCompanyType(param.getCompanyType());
        project.setUserType(param.getUserType());
        if (StringUtils.isNotBlank(param.getEditorId()) && StringUtils.isNumeric(param.getEditorId())) {
            project.setEditorId(Long.valueOf(param.getEditorId()));
        }
        project.setEditorAccount(project.getCreatorAccount());
        project.setUpdateTime(System.currentTimeMillis() / 1000);
        return projectMapper.updateByPrimaryKeySelective(project) == 1;
    }

    @Override
    public boolean validateDeviceIdIsExist(String deviceId) {
        boolean returnValue = true;
        ProjectExample example = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(deviceId) && StringUtils.isNumeric(deviceId)) {
            criteria.andDeviceIdEqualTo(Long.valueOf(deviceId));
        }
        List<Project> list = projectMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            returnValue = false;
        }
        return returnValue;
    }

    @Override
    public Project selectById(Long id) {
        return projectMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Project> selectByAppId(Long appId) {
        ProjectExample example = new ProjectExample();
        example.createCriteria().andAppIdEqualTo(appId);
        int pageSize = projectMapper.countByExample(example);
        if (pageSize < 1) {
            return Collections.emptyList();
        }
        example.setLimitStart(0);
        example.setLimitEnd(pageSize);
        return projectMapper.selectByExample(example);
    }

    @Override
    public List<Project> selectByCompanyId(Long companyId) {
        ProjectExample example = new ProjectExample();
        example.createCriteria().andCompanyIdEqualTo(companyId);
        int pageSize = projectMapper.countByExample(example);
        if (pageSize < 1) {
            return Collections.emptyList();
        }
        example.setLimitStart(0);
        example.setLimitEnd(pageSize);
        return projectMapper.selectByExample(example);
    }

    @Override
    public List<ProjectBo> selectByProjectSelectParam(ProjectSelectParam param) {
        //根据前端所传参数不同生成对应的orderBy子句
        String orderByClause = AdminConstant.getOrderByClauseMap().getOrDefault(param.getSortField(), AdminConstant.CREATE_TIME)
                + AdminConstant.getOrderByClauseSequenceMap().getOrDefault(param.getOrder(), AdminConstant.DESC);
        if(StringUtils.isEmpty(param.getSortField()) || (param.getSortField().equals("createTime")||param.getSortField().equals("updateTime"))){
            orderByClause = " a."+orderByClause.substring(1,orderByClause.length());
        }
        Long appId = null;
        Long companyId = null;
        //如果 searchCompanyId 字段不为空，则匹配公司ID进行查询
        if (StringUtils.isNotBlank(param.getSearchCompanyId())
                && StringUtils.isNumeric(param.getSearchCompanyId()) && !Long.valueOf(param.getSearchCompanyId()).equals(0L)) {
            companyId = Long.valueOf(param.getSearchCompanyId());
        }
        //如果 searchAppId 字段不为空，则匹配appId进行查询
        if (StringUtils.isNotBlank(param.getSearchAppId())
                && StringUtils.isNumeric(param.getSearchAppId()) && !Long.valueOf(param.getSearchAppId()).equals(0L)) {
            appId = Long.valueOf(param.getSearchAppId());
        }
        Integer pageIndex = (param.getPageIndex() - 1) * param.getPageSize();
        return projectMapper.selectProjectInfoByParam(appId, companyId, param.getSearchKeyword(), pageIndex, param.getPageSize(), orderByClause);
    }

    @Override
    public int countByProjectSelectParam(ProjectSelectParam param) {
        Long appId = null;
        Long companyId = null;
        //如果 searchCompanyId 字段不为空，则匹配公司ID进行查询
        if (StringUtils.isNotBlank(param.getSearchCompanyId())
                && StringUtils.isNumeric(param.getSearchCompanyId()) && !Long.valueOf(param.getSearchCompanyId()).equals(0L)) {
            companyId = Long.valueOf(param.getSearchCompanyId());
        }
        //如果 searchAppId 字段不为空，则匹配appId进行查询
        if (StringUtils.isNotBlank(param.getSearchAppId())
                && StringUtils.isNumeric(param.getSearchAppId()) && !Long.valueOf(param.getSearchAppId()).equals(0L)) {
            appId = Long.valueOf(param.getSearchAppId());
        }
        return projectMapper.countByProjectParam(appId, companyId, param.getSearchKeyword());
    }

    private ProjectExample getProjectSelectParamExample(ProjectSelectParam param) {
        ProjectExample example = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
        //如果 searchCompanyId 字段不为空，则匹配公司ID进行查询
        if (StringUtils.isNotBlank(param.getSearchCompanyId())
                && StringUtils.isNumeric(param.getSearchCompanyId()) && !Long.valueOf(param.getSearchCompanyId()).equals(0L)) {
            criteria.andCompanyIdEqualTo(Long.valueOf(param.getSearchCompanyId()));
        }
        //如果 searchAppId 字段不为空，则匹配appId进行查询
        if (StringUtils.isNotBlank(param.getSearchAppId())
                && StringUtils.isNumeric(param.getSearchAppId()) && !Long.valueOf(param.getSearchAppId()).equals(0L)) {
            criteria.andAppIdEqualTo(Long.valueOf(param.getSearchAppId()));
        }
        //根据 公司名称 模糊搜索
        if (StringUtils.isNotBlank(param.getSearchKeyword())) {
            criteria.andCompanyNameLike("%" + param.getSearchKeyword() + "%");
        }
        return example;
    }

}